﻿using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace HTR.Model
{
    public class CotaTerreno
    {
        
        #region Propiedades

        private string _texto;
        public string Texto
        {
            get { return _texto; }
            set { _texto = value; }
        }

        private bool _existente;
        public bool Existente
        {
            get { return _existente; }
            set { _existente = value; }
        }

        private Point3d _puntoInsercion;
        public Point3d PuntoInsercion
        {
            get { return _puntoInsercion; }
            set { _puntoInsercion = value; }
        }

        private string _capa;
        public string Capa
        {
            get { return _capa; }
            set { _capa = value; }
        }

        private DBText _acText;

        #endregion // Propiedades


        #region Contructores

        public CotaTerreno()
        {
            this._acText = new DBText();
            this._existente = false;
            this._acText.Rotation = Math.PI / 4.0;
            this._acText.Height = 1.7;
        }

        public CotaTerreno(Point3d puntoInsercion, string texto, bool existente)
        {
            this._acText = new DBText();
            this._existente = existente;
            double num;
            if (double.TryParse(texto, out num))
                this._acText.TextString = num.ToString();
            else //Es existente
                this._acText.TextString = texto;
            this._acText.Position = puntoInsercion.Add(new Vector3d(3 * Math.Cos(Math.PI / 4.0), 3 * Math.Sin(Math.PI / 4.0), 0)); // Ubica el punto de inserción del texto a una distancia de 3 a 45 grados
            this._acText.Rotation = Math.PI / 4.0; // Ubica el texto a 45 grados
            this._acText.Height = 1.7; // Establece la altura del texto, actualmente está para escala 1/1000
        }

        #endregion // Contructores


        #region Metodos

        public void Dibujar(BlockTableRecord acBlkTblRec, Transaction acTrans, LayerTable acLyrTbl, string layerName)
        {
            if (!acLyrTbl.Has(layerName))
            {
                LayerTableRecord acLyrTblRec = new LayerTableRecord();
                acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
                acLyrTblRec.Name = layerName;

                // Upgrade the Layer table for write
                acLyrTbl.UpgradeOpen();

                // Append the new layer to the Layer table and the transaction
                acLyrTbl.Add(acLyrTblRec);
                acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
            }
            this._acText.Layer = layerName;
            acBlkTblRec.AppendEntity(this._acText);
            acTrans.AddNewlyCreatedDBObject(this._acText, true);
        }

        public void Modificar(Transaction acTrans, string NuevoValor)
        {
            DBText tempText = acTrans.GetObject(this._acText.ObjectId, OpenMode.ForWrite) as DBText;
            tempText.TextString = NuevoValor;
            this._acText = tempText;
        }

        public void Borrar(Transaction acTrans)
        {
            DBText tempText = acTrans.GetObject(this._acText.ObjectId, OpenMode.ForWrite) as DBText;
            tempText.Erase();
            this._acText = null;
        }

        #endregion // Metodos

    }
}
